package com.chen.service.impl;

import com.chen.client.Service1Client;
import com.chen.client.Service2Client;
import com.chen.service.IBusinessService;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hmily.annotation.HmilyTCC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Slf4j
@Service
public class BusinessServiceImpl implements IBusinessService {

    @Autowired
    private Service1Client service1Client;

    @Autowired
    private Service2Client service2Client;

    @Override
    @HmilyTCC(confirmMethod = "commitTrans",cancelMethod = "cancelTrans")
    public void transferA2B(int accountId1, int accountId2, BigDecimal amount) {
        service1Client.serviceOneTransOut(accountId1,amount);
        service2Client.transferInto(accountId2,amount);
    }

    @Override
    @HmilyTCC(confirmMethod = "commitTrans",cancelMethod = "cancelTrans")
    public void transferA2BWithException(int accountId1, int accountId2, BigDecimal amount) {
        service1Client.serviceOneTransOut(accountId1,amount);
        service2Client.transferIntoWithException(accountId2,amount);
    }

    public void commitTrans(int accountId1, int accountId2, BigDecimal amount) {
        //
        log.info("{}向{}转账{}，成功！",accountId1,accountId2,amount);
    }
    public void cancelTrans(int accountId1, int accountId2, BigDecimal amount) {
        log.error("{}向{}转账{}，失败！",accountId1,accountId2,amount);
    }
}
